package com.cyj.dream.admin.service.system.impl;

import cn.hutool.core.util.ObjectUtil;
import com.cyj.dream.admin.model.system.SysModule;
import com.cyj.dream.admin.model.system.SysRoleModule;
import com.cyj.dream.admin.model.vo.LeftMenuVo;
import com.cyj.dream.admin.persistence.manager.systems.SysModuleManager;
import com.cyj.dream.admin.persistence.manager.systems.SysRoleModuleManager;
import com.cyj.dream.admin.service.system.SysModuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description: 系统菜单模块服务实现类
 * @BelongsProject: dream-DreamChardonnay
 * @BelongsPackage: com.dream.DreamChardonnay.service.impl
 * @Author: ChenYongJia
 * @CreateTime: 2021-04-29 09:49
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@Service
public class SysModuleServiceImpl implements SysModuleService {

    @Autowired
    private SysModuleManager sysModuleManager;

    @Autowired
    private SysRoleModuleManager sysRoleModuleManager;

    /**
     * 根据菜单对象信息查询用户菜单树
     *
     * @param sysModule
     * @return java.util.List<com.dream.DreamChardonnay.model.system.SysModule>
     * @date 2021/5/6 9:54
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public List<SysModule> selectSysModuleTree(SysModule sysModule) {
        return sysModuleManager.selectSysModuleTree(sysModule);
    }

    /**
     * 根据用户查询的左侧权限菜单栏
     *
     * @return java.util.List<com.dream.DreamChardonnay.model.vo.LeftMenuVo>
     * @date 2021-6-8 09:57:24
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public List<LeftMenuVo> selectParentSysModule() {
        return sysModuleManager.selectParentSysModule();
    }

    /**
     * 根据菜单模块名称查询菜单信息
     *
     * @param sysModule 菜单名称
     * @return com.dream.DreamChardonnay.model.system.SysModule
     * @date 2021/5/6 8:57
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public SysModule selectBySysModuleName(SysModule sysModule) {
        return sysModuleManager.selectBySysModuleName(sysModule);
    }

    /**
     * 根据菜单id查询菜单信息
     *
     * @param sysModuleId 菜单id
     * @return com.dream.DreamChardonnay.model.system.SysModule
     * @date 2021/4/30 9:59
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public SysModule selectModuleByModuleId(Long sysModuleId) {
        return sysModuleManager.selectModuleByModuleId(sysModuleId);
    }

    /**
     * 根据菜单id查询菜单信息
     *
     * @param sysModulesParentId 菜单id
     * @return java.util.List<com.dream.DreamChardonnay.model.system.SysModule>
     * @date 2021/5/6 10:01
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public List<SysModule> selectSysModuleParent(Long sysModulesParentId) {
        return sysModuleManager.selectSysModuleParent(sysModulesParentId);
    }

    /**
     * 添加系统菜单信息
     *
     * @param sysModule 菜单对象
     * @return boolean
     * @date 2021/4/30 9:56
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public boolean addModuleInfo(SysModule sysModule) {
        return sysModuleManager.addModuleInfo(sysModule);
    }

    /**
     * 修改系统菜单信息
     *
     * @param sysModule 菜单对象
     * @return boolean
     * @date 2021/4/30 9:57
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public boolean updateModuleInfo(SysModule sysModule) {
        return sysModuleManager.updateModuleInfo(sysModule);
    }

    /**
     * 逻辑删除菜单信息
     *
     * @param sysModule 菜单对象
     * @return boolean
     * @date 2021/4/30 9:58
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public boolean deleteModuleInfo(SysModule sysModule) {
        return sysModuleManager.deleteModuleInfo(sysModule);
    }


    /**
     * 获取所有模块信息
     *
     * @return java.util.List<com.dream.DreamChardonnay.model.system.SysModule>
     * @date 2021/6/10 10:44
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public List<SysModule> getModulesLists() {
        return sysModuleManager.getModulesLists();
    }

    /**
     * 根据角色查询该角色拥有的模块
     *
     * @param sysRoleId 角色id
     * @return java.util.List<com.dream.DreamChardonnay.model.system.SysModule>
     * @date 2021/6/10 10:45
     * @author ChenYongJia
     * @version 1.0
     */
    @Override
    public List<SysModule> getRoleModulesByRoleId(Long sysRoleId) {
        SysRoleModule sysRoleModule = new SysRoleModule();
        sysRoleModule.setSysRoleId(sysRoleId);

        Set<Long> sysModuleId = new HashSet<>();
        List<SysRoleModule> sysRoleModules = sysRoleModuleManager.selectBySysRoleModule(sysRoleModule);
        if (!ObjectUtil.isEmpty(sysRoleModules)) {
            sysRoleModules.stream().forEach(item -> {
                sysModuleId.add(item.getSysModuleId());
            });
            return sysModuleManager.selectSysModuleBySysModuleIdList(sysModuleId);
        } else {
            return null;
        }
    }


}
